#pragma once

#include <sys/epoll.h>
#include <cstring>
#include "LogMessage.hpp"
#include <iostream>

class Epoll {
public:
  static int CreateEpoll() {
    int res = epoll_create(128);
    if(res == -1) {
      LogMessage(FATAL, "create epoll fail, errno:[%d]::%s", errno, strerror(errno));
      exit(5);
    }
    
    // return epoll`s mode fd
    return res;
  }

  static bool CtlEpoll(int epfd, int op, int fd, uint32_t events) {
    epoll_event event;
    event.data.fd = fd;
    event.events = events;

    int res = epoll_ctl(epfd, op, fd, &event);
    if(res == -1) {
      LogMessage(FATAL, "epoll_ctl fail, errno[%d]::%s", errno, strerror(errno));
      exit(6);
    }

    return res == 0;
  }
  

  static int WaitEpoll(int epfd, epoll_event *events, int maxevents, int timeout) {
    int res = epoll_wait(epfd, events, maxevents, timeout);
    if(res == -1) {
      LogMessage(FATAL, "epoll_wait fail, errno[%d]::%s", errno, strerror(errno));
      exit(7);
    }
    return res;
  } 
};
